package org.budo.redis.repository.jedis;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.budo.redis.repository.BudoRedisRepository;
import org.budo.redis.repository.util.BudoRedisGraphUtil;
import org.budo.support.lang.util.MapUtil;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @author limingwei
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Slf4j
public class BudoJedisRepository implements BudoRedisRepository {
    private JedisPool jedisPool;

    private JedisPool jedisPool() {
        if (null == this.getJedisPool()) {
            throw new RuntimeException("#33 this.jedisPool=" + this.getJedisPool() + ", this=" + this);
        }

        return this.getJedisPool();
    }

    @Override
    public Long del(String key) {
        return this.del(new String[] { key });
    }

    @Override
    public Long hdel(String key, String field) {
        return this.hdel(key, new String[] { field });
    }

    @Override
    public Long del(String[] keys) {
        Jedis jedis = this.jedisPool().getResource();
        try {
            return jedis.del(keys);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long hdel(String key, String[] fields) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.hdel(key, fields);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public List<String> mget(String[] keys) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.mget(keys);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public String mset(Map<String, String> keysValues) {
        String[] pairs = MapUtil.toPairs(keysValues);
        return this.mset(pairs);
    }

    @Override
    public Long msetnx(Map<String, String> keysValues) {
        return this.msetnx(MapUtil.toPairs(keysValues));
    }

    @Override
    public String mset(String[] keysValues) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.mset(keysValues);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long msetnx(String[] keysValues) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.msetnx(keysValues);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Boolean exists(String key) {
        Jedis jedis = this.jedisPool().getResource();
        try {
            return jedis.exists(key);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long expire(String key, Number seconds) {
        Jedis jedis = this.jedisPool().getResource();
        int intValue = seconds.intValue();

        try {
            return jedis.expire(key, intValue);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long expire(byte[] key, Number seconds) {
        Jedis jedis = this.jedisPool().getResource();
        int intValue = seconds.intValue();

        try {
            return jedis.expire(key, intValue);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long expireAt(String key, Date at) {
        Jedis jedis = this.jedisPool().getResource();
        long milliSeconds = at.getTime();
        long unixTime = milliSeconds / 1000;

        try {
            return jedis.expireAt(key, unixTime);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Set<String> keys(String pattern) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.keys(pattern);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long decr(String key) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.decr(key);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long decrBy(String key, Number value) {
        Jedis jedis = this.jedisPool().getResource();
        long longValue = value.longValue();

        try {
            return jedis.decrBy(key, longValue);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public String get(String key) {
        return BudoRedisGraphUtil.redisGraph().get(this.jedisPool(), key);
    }

    @Override
    public byte[] get(byte[] key) {
        return BudoRedisGraphUtil.redisGraph().get_by_bytes(this.jedisPool(), key);
    }

    @Override
    public String set(String key, String value) {
        return BudoRedisGraphUtil.redisGraph().set(this.jedisPool(), key, value);
    }

    @Override
    public String getSet(String key, String value) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.getSet(key, value);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long incr(String key) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.incr(key);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long incrBy(String key, Number value) {
        Jedis jedis = this.jedisPool().getResource();
        long longValue = value.longValue();

        try {
            return jedis.incrBy(key, longValue);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Double incrByFloat(String key, Number value) {
        Jedis jedis = this.jedisPool().getResource();
        double doubleValue = value.doubleValue();

        try {
            return jedis.incrByFloat(key, doubleValue);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public String set(byte[] key, byte[] value) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.set(key, value);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public String setex(String key, Number seconds, String value) {
        Jedis jedis = this.jedisPool().getResource();
        int intValue = seconds.intValue();

        try {
            return jedis.setex(key, intValue, value);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public String setex(byte[] key, Number seconds, byte[] bytes) {
        Jedis jedis = this.jedisPool().getResource();
        int intValue = seconds.intValue();

        try {
            return jedis.setex(key, intValue, bytes);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Boolean hexists(String key, String field) {
        Jedis jedis = this.jedisPool().getResource();
        try {
            return jedis.hexists(key, field);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public String hget(String key, String field) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.hget(key, field);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Map<String, String> hgetAll(String key) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.hgetAll(key);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long hincrBy(String key, String field, Number value) {
        Jedis jedis = this.jedisPool().getResource();
        long longValue = value.longValue();

        try {
            return jedis.hincrBy(key, field, longValue);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Double hincrByFloat(String key, String field, Number value) {
        Jedis jedis = this.jedisPool().getResource();
        double doubleValue = value.doubleValue();

        try {
            return jedis.hincrByFloat(key, field, doubleValue);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Set<String> hkeys(String key) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.hkeys(key);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long hlen(String key) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.hlen(key);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public List<String> hmget(String key, String[] fields) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.hmget(key, fields);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public String hmset(String key, Map<String, String> hash) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.hmset(key, hash);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long hset(String key, String field, String value) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.hset(key, field, value);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long hsetnx(String key, String field, String value) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.hsetnx(key, field, value);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public List<String> hvals(String key) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.hvals(key);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public List<String> blpop(Number timeout, String[] keys) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.blpop(timeout.intValue(), keys);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public List<String> brpop(Number timeout, String[] keys) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.brpop(timeout.intValue(), keys);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public String brpoplpush(String source, String destination, Number timeout) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.brpoplpush(source, destination, timeout.intValue());
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public String lindex(String key, Number index) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.lindex(key, index.longValue());
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long linsert(String key, Boolean before, String pivot, String value) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            LIST_POSITION where = before ? LIST_POSITION.BEFORE : LIST_POSITION.AFTER;
            return jedis.linsert(key, where, pivot, value);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long llen(String key) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.llen(key);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public String lpop(String key) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.lpop(key);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long lpush(String key, String[] values) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.lpush(key, values);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long lpushx(String key, String[] values) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.lpushx(key, values);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public List<String> lrange(String key, Number start, Number end) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.lrange(key, start.longValue(), end.longValue());
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long lrem(String key, Number count, String value) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.lrem(key, count.longValue(), value);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public String lset(String key, Number index, String value) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.lset(key, index.longValue(), value);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public String ltrim(String key, Number start, Number end) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.ltrim(key, start.longValue(), end.longValue());
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public String rpop(String key) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.rpop(key);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public String rpoplpush(String srckey, String dstkey) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.rpoplpush(srckey, dstkey);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long rpush(String key, String[] values) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.rpush(key, values);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long rpushx(String key, String[] values) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.rpushx(key, values);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long sadd(String key, String[] members) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.sadd(key, members);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long scard(String key) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.scard(key);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Set<String> sdiff(String[] keys) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.sdiff(keys);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long sdiffstore(String dstkey, String[] keys) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.sdiffstore(dstkey, keys);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Set<String> sinter(String[] keys) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.sinter(keys);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long sinterstore(String dstkey, String[] keys) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.sinterstore(dstkey, keys);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Boolean sismember(String key, String member) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.sismember(key, member);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Set<String> smembers(String key) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.smembers(key);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long smove(String srckey, String dstkey, String member) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.smove(srckey, dstkey, member);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public String spop(String key) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.spop(key);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public List<String> srandmember(String key, Number count) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.srandmember(key, count.intValue());
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long srem(String key, String[] members) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.srem(key, members);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Set<String> sunion(String[] keys) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.sunion(keys);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long sunionstore(String dstkey, String[] keys) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.sunionstore(dstkey, keys);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long zadd(String key, Number score, String member) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.zadd(key, score.doubleValue(), member);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long zadd(String key, Map<String, Double> scoreMembers) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.zadd(key, scoreMembers);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long zcard(String key) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.zcard(key);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long zcount(String key, Number min, Number max) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.zcount(key, min.doubleValue(), max.doubleValue());
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Double zincrby(String key, Number score, String member) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.zincrby(key, score.doubleValue(), member);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Set<String> zrange(String key, Number start, Number end) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.zrange(key, start.longValue(), end.longValue());
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Set<String> zrangeByScore(String key, Number min, Number max) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.zrangeByScore(key, min.doubleValue(), max.doubleValue());
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long zrank(String key, String member) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.zrank(key, member);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long zrem(String key, String[] members) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.zrem(key, members);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long zremrangeByRank(String key, Number start, Number end) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.zremrangeByRank(key, start.longValue(), end.longValue());
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long zremrangeByScore(String key, Number start, Number end) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.zremrangeByScore(key, start.doubleValue(), end.doubleValue());
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Set<String> zrevrange(String key, Number start, Number end) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.zrevrange(key, start.longValue(), end.longValue());
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Set<String> zrevrangeByScore(String key, Number max, Number min) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.zrevrangeByScore(key, max.doubleValue(), min.doubleValue());
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long zrevrank(String key, String member) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.zrevrank(key, member);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Double zscore(String key, String member) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.zscore(key, member);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Set<String> zrangeByLex(String key, String min, String max) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.zrangeByLex(key, min, max);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Set<String> zrangeByLex(String key, String min, String max, Number offset, Number count) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.zrangeByLex(key, min, max, offset.intValue(), count.intValue());
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long zlexcount(String key, String min, String max) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.zlexcount(key, min, max);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long zremrangeByLex(String key, String min, String max) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.zremrangeByLex(key, min, max);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Object eval(String script) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.eval(script);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Object eval(String script, List<String> keys, List<String> args) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.eval(script, keys, args);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Object eval(String script, Number keyCount, String[] params) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.eval(script, keyCount.intValue(), params);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public String select(Number index) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.select(index.intValue());
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public String ping() {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.ping();
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public String echo(String message) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.echo(message);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public String auth(String password) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.auth(password);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public List<String> time() {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.time();
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long lastsave() {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.lastsave();
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public String info() {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.info();
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public String info(String section) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.info(section);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long dbSize() {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.dbSize();
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public List<String> configGet(String pattern) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.configGet(pattern);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public String clientSetname(String name) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.clientSetname(name);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public String clientList() {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.clientList();
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public String clientGetname() {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.clientGetname();
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Integer getInteger(String key) {
        String value = this.get(key);
        if (null == value || value.isEmpty() || value.equalsIgnoreCase("null")) {
            return null;
        }

        return new Integer(value);
    }

    private void closeJedis(Jedis jedis) {
        if (null == jedis) {
            return;
        }

        try {
            jedis.close();
        } catch (Throwable e) {
            log.error("#89 closeJedis error, e=" + e + ", jedis=" + jedis, e);
        }
    }

    @Override
    public String toString() {
        return super.toString() + ", jedisPool=" + this.getJedisPool();
    }

    @Override
    public Long setnx(String key, String value) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.setnx(key, value);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long setnx(String key, String value, Number expireInSeconds) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            Long setnx = jedis.setnx(key, value);

            // 如果成功
            if (null != setnx && setnx > 0) {
                jedis.expire(key, expireInSeconds.intValue()); // 设置过期时间
            }

            return setnx;
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long ttl(String key) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.ttl(key);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long pttl(String key) {
        Jedis jedis = this.jedisPool().getResource();

        try {
            return jedis.pttl(key);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public Long del(byte[]... keys) {
        throw new RuntimeException();
    }
}